<template>
  <div class="app-container">
    <el-row>
      <el-col :span="6">
        <el-button size="mini" type="primary" @click="changeIsAdd" style="margin-bottom: 5px">新增分类</el-button>
        <el-tree
          :props="props"
          :data="rows"
          node-key="id"
          draggable
          :highlight-current="true"
          :accordion="false"
          :default-expand-all="false"
          :expand-on-click-node="false"
          @node-click="changeIsUpdate"
          @node-drag-start="handleDragStart"
          @node-drag-enter="handleDragEnter"
          @node-drag-leave="handleDragLeave"
          @node-drag-over="handleDragOver"
          @node-drag-end="handleDragEnd"
          @node-drop="handleDrop"
        />
      </el-col>
      <el-col :span="12">
        <el-form
          v-if="isAdd !== undefined"
          ref="form"
          label-width="120px"
        >
          <el-form-item label="分类名称">
            <el-input v-model="form.name"/>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-radio-group v-model="form.hidden">
              <el-radio :label="false">显示</el-radio>
              <el-radio :label="true">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="showPropertyDialog">
              属性
            </el-button>
            <el-button type="primary" @click="saveAll">
              <span v-if="isAdd">
                新增
              </span>
              <span v-else>
                修改
              </span>
            </el-button>
            <el-button v-if="!isAdd" type="danger" @click="del">
              删除
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-dialog
      title="属性"
      :visible.sync="propertyDialog"
      :before-close="dialogClose"
    >
      <el-tag
        :key="tag.name"
        v-for="tag in form.properties"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag.name}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="property"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button>

    </el-dialog>
  </div>
</template>

<script>
import { saveAll, list } from '@/api/category'
import { commonDel, commonList, commonSaveAll } from '@/utils/common'

export default {
  name: 'Category',
  data() {
    return {
      property:'',
      inputVisible: false,
      propertyDialog: false,
      isAdd: undefined,
      rows: [],
      props: {
        label: 'name',
        children: 'children'
      },
      form: {},
      row: {}
    }
  },
  beforeMount() {
    this.resetForm()
    this.list()
  },
  methods: {
    showInput() {
      this.inputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleClose(tag) {
      this.form.properties.splice(this.form.properties.indexOf(tag), 1);
      this.saveAll()
    },
    handleInputConfirm() {
      if (this.property) {
        this.form.properties.push({name:this.property,categoryId: this.form.id})
        this.saveAll()
      }
      this.inputVisible = false
      this.property = ''
    },
    dialogClose() {
      this.propertyDialog = false
      this.$nextTick(() => {
        this.inputVisible = false
      });

    },
    showPropertyDialog() {
      this.propertyDialog = true
    },
    changeIsAdd() {
      this.isAdd = true
      this.resetForm()
      this.form.sort = this.rows[this.rows.length - 1].sort
    },
    changeIsUpdate(menu) {
      this.form = menu
      this.isAdd = false
    },
    resetForm() {
      this.form = {
        id: '',
        name: '',
        hidden: false,
        children: [],
        properties:[],
      }
    },
    async saveAll() {
      this.row = this.form
      await commonSaveAll(this, saveAll ,this.list)
    },
    del() {
      this.row = this.form
      commonDel(this)
    },
    list() {
     commonList(this.rows,list)
    },
    handleDragStart(node) {
      console.log('drag start', node)
    },
    handleDragEnter(draggingNode, dropNode) {
      console.log('tree drag enter: ', dropNode.label)
    },
    handleDragLeave(draggingNode, dropNode) {
      console.log('tree drag leave: ', dropNode.label)
    },
    handleDragOver(draggingNode, dropNode) {
      console.log('tree drag over: ', dropNode.label)
    },
    handleDragEnd(draggingNode, dropNode, dropType) {
      console.log('tree drag end: ', dropNode && dropNode.label, dropType)
    },
    handleDrop(draggingNode, dropNode, dropType) {
      let draggingNodeData = draggingNode.data
      let dropNodeData = dropNode.data
      if (dropType == 'inner') {
        let temp = JSON.parse(JSON.stringify(dropNodeData))
        temp.children = undefined
        draggingNodeData.parent = temp
      } else {
        draggingNodeData.parent = dropNodeData.parent
        if (dropType == 'after') {
          draggingNodeData.sort = dropNodeData.sort + 1
        } else if (dropType == 'before') {
          draggingNodeData.sort = dropNodeData.sort - 1
        }
      }
      saveAll({ data: JSON.stringify([draggingNodeData, dropNodeData]) })
    }
  }
}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
